//
//  SportSwingTableViewCell.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 15/12/16.
//  Copyright © 2015年 CoolLang. All rights reserved.
//

#import "SportSwingTableViewCell.h"

#define kBarHeight (self.kBarView.bounds.size.height - 25)

@interface SportSwingTableViewCell ()
@property (strong, nonatomic) IBOutletCollection(NSLayoutConstraint) NSArray *barHegihtConstraint;
@property (weak, nonatomic) IBOutlet UIView *kBarView;
@property (strong, nonatomic) NSArray * barHeightArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pingJiHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xuanQiuHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *xueQiuHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *jieJiHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *kouShaHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *faQiuHeight;
@property (weak, nonatomic) IBOutlet UILabel *pingJiCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *xuanQiuCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *xueQiuCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *jieJiCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *kouShaCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *faQiuCountLabel;
@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;

@property (weak, nonatomic) IBOutlet UILabel *todaySwingDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *shotsUnitLabel;

@end

@implementation SportSwingTableViewCell

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    _barHeightArray = @[_pingJiHeight,_xuanQiuHeight,_xueQiuHeight,_jieJiHeight,_kouShaHeight,_faQiuHeight];
    for (NSInteger index = 0; index < 6; index++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:index + 10];
        imageView.image = [imageView.image resizableImageWithCapInsets:UIEdgeInsetsMake(12, 0, 1, 0)];
    }
    
    self.todaySwingDescriptionLabel.text = NSLocalizedString(@"Today", nil);
    self.shotsUnitLabel.text = NSLocalizedString(@"Sport.Unit.Shots", nil);
}

- (void)configureBarHeightWithSportRecordItem:(SportRecordItem *)item
{
    self.totalCountLabel.text = [NSString stringWithFormat:@"%ld",(long)[item amountOfCount]];
    if (![item.date isToday]) {
        self.todaySwingDescriptionLabel.text = NSLocalizedString(@"Today", nil);
    }else {
        self.todaySwingDescriptionLabel.text = [item yearAndMonthAndDay];
    }
    
    NSDictionary *imageNameDic = @{
                                   @"pingJiCount": @"sport_icon_pingji",
                                   @"xuanQiuCount": @"sport_icon_xuanqiu",
                                   @"xueQiuCount": @"sport_icon_xueqiu",
                                   @"jieJiCount": @"sport_icon_jieji",
                                   @"kouShaCount": @"sport_icon_kousha",
                                   @"faQiuCount": @"sport_icon_faqiu"
                                   };
    
    NSDictionary *titleNameDic = @{
                                   @"pingJiCount": NSLocalizedString(@"Flat", nil),
                                   @"xuanQiuCount": NSLocalizedString(@"Topspin", nil),
                                   @"xueQiuCount": NSLocalizedString(@"Slice", nil),
                                   @"jieJiCount": NSLocalizedString(@"Volley", nil),
                                   @"kouShaCount": NSLocalizedString(@"Smash", nil),
                                   @"faQiuCount": NSLocalizedString(@"Serve", nil)
                                   };
    
    NSMutableArray *valueArray = @[
  @{
      @"type":@"pingJiCount",
      @"value":@(item.pingJiCount)
    },
  @{
      @"type":@"xuanQiuCount",
      @"value":@(item.xuanQiuCount)
      },
  @{
      @"type":@"xueQiuCount",
      @"value":@(item.xueQiuCount)
      },
  @{
      @"type":@"jieJiCount",
      @"value":@(item.jieJiCount)
      },
  @{
      @"type":@"kouShaCount",
      @"value":@(item.kouShaCount)
      },
  @{
      @"type":@"faQiuCount",
      @"value":@(item.faQiuCount)
      }
  ].mutableCopy;
    [valueArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1[@"value"] integerValue] > [obj2[@"value"] integerValue];
    }];
    
    for (NSInteger index = 0; index < 6; index++) {
        UIImageView *barImageView = (UIImageView *)[self.kBarView viewWithTag:10 + index];
        NSString *key = valueArray[index][@"type"];
        [barImageView setImage:[[UIImage imageNamed:imageNameDic[key]] resizableImageWithCapInsets:UIEdgeInsetsMake(12, 0, 1, 0)]];
        
        UILabel *valueLabel = (UILabel *)[self.kBarView viewWithTag:100 + index];
        valueLabel.text = [NSString stringWithFormat:@"%@",valueArray[index][@"value"]];
        
        UILabel *titleNameLabel = (UILabel *)[self.kBarView viewWithTag:1000 + index];
        titleNameLabel.text = titleNameDic[key];
    }
    
    NSInteger maxSwings = 0;
    for (NSDictionary *poseDic in valueArray) {
        if ([poseDic[@"value"] integerValue] > maxSwings) {
            maxSwings = [poseDic[@"value"] integerValue];
        }
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        NSInteger index = 0;
        for (NSDictionary * value in valueArray) {
            NSLayoutConstraint * barHeightConstraint = self.barHeightArray[index];
            //13是基础高度
            if (maxSwings != 0) {
                barHeightConstraint.constant = 13 + [value[@"value"] integerValue]/(CGFloat)maxSwings * kBarHeight;
            } else {
                barHeightConstraint.constant = 13;
            }
            index++;
        }
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

@end
